/******************************************************************************
 * Copyright (C) 2017 Chengdu Dlhd information industry co. LTD.
 * All Rights Reserved.
 * 本软件为成都顶联互动信息产业有限公司开发研制。未经本公司正式书面同意，其他任何个人、团体不得使用、复制、修改或发布本软件.
 *****************************************************************************/
package net.dgg.dggcloud.service.jrcrm.service.business;

import com.alibaba.fastjson.JSONObject;
import net.dgg.dggcloud.core.cache.CacheManager;
import net.dgg.dggcloud.core.constants.Constants;
import net.dgg.dggcloud.core.json.JsonUtil;
import net.dgg.dggcloud.core.util.ConfigLoader;
import net.dgg.dggcloud.core.util.DateUtil;
import net.dgg.dggcloud.core.util.StringUtils;
import net.dgg.dggcloud.service.common.base.service.BaseService;
import net.dgg.dggcloud.service.common.exception.BizException;
import net.dgg.dggcloud.service.common.pagination.PageData;
import net.dgg.dggcloud.service.entity.user.User;
import net.dgg.dggcloud.service.iboss.entity.ScProOrder;
import net.dgg.dggcloud.service.jrcrm.constant.BusinessConstant;
import net.dgg.dggcloud.service.jrcrm.constant.CommonConstant;
import net.dgg.dggcloud.service.jrcrm.constant.CustomerConstant;
import net.dgg.dggcloud.service.jrcrm.constant.CustomerRecordConstant;
import net.dgg.dggcloud.service.jrcrm.dao.business.BusinessDao;
import net.dgg.dggcloud.service.jrcrm.dao.business.SourceBusinessDao;
import net.dgg.dggcloud.service.jrcrm.dao.customer.ContactDao;
import net.dgg.dggcloud.service.jrcrm.entity.business.AntiInvalid;
import net.dgg.dggcloud.service.jrcrm.entity.business.Business;
import net.dgg.dggcloud.service.jrcrm.entity.business.BusinessAllotCount;
import net.dgg.dggcloud.service.jrcrm.entity.business.SourceBusiness;
import net.dgg.dggcloud.service.jrcrm.entity.cms.ChannelCost;
import net.dgg.dggcloud.service.jrcrm.entity.cms.TreeBook;
import net.dgg.dggcloud.service.jrcrm.entity.cms.UserSetting;
import net.dgg.dggcloud.service.jrcrm.entity.customer.Contact;
import net.dgg.dggcloud.service.jrcrm.entity.customer.Customer;
import net.dgg.dggcloud.service.jrcrm.entity.customer.DepartmentCustomer;
import net.dgg.dggcloud.service.jrcrm.entity.customer.WhiteList;
import net.dgg.dggcloud.service.jrcrm.service.cms.*;
import net.dgg.dggcloud.service.jrcrm.service.customer.DepartmentCustomerService;
import net.dgg.dggcloud.service.jrcrm.service.customer.WhiteListService;
import net.dgg.dggcloud.service.jrcrm.service.customer.WidelyResourceService;
import net.dgg.dggcloud.service.jrcrm.service.customer.api.ContactApiService;
import net.dgg.dggcloud.service.jrcrm.service.customer.api.CustomerApiService;
import net.dgg.dggcloud.service.jrcrm.utils.BizExceptionUtil;
import net.dgg.dggcloud.service.jrcrm.utils.HttpRequest;
import net.dgg.dggcloud.service.jrcrm.vo.BusinessAddVo;
import net.dgg.dggcloud.service.service.RoleService;
import net.dgg.dggcloud.service.service.organization.OrganizationService;
import net.dgg.dggcloud.service.service.user.UserService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.util.HtmlUtils;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * @ClassName 〈SourceBusinessService>
 * @Description
 * @Author 朱永波
 * @Date 2017/11/3 11:42
 */
@Service
public class SourceBusinessService extends BaseService {

    @Resource
    private SourceBusinessDao sourceBusinessDao;

    /**
     * 根据条件统计商机数量
     *
     * @param map
     * @return
     */
    public Integer countSourceBusinessByMap(Map map) {
        return sourceBusinessDao.countSourceBusinessByMap(map);
    }


    /**
     * 根据条件查询商机列表
     *
     * @param map
     * @return
     */
    public List<Map> listSourceBusinessByMap(Map map) {
        return sourceBusinessDao.listSourceBusinessByMap(map);
    }


    public SourceBusiness getById(Long id) {
        return repository.get(SourceBusiness.class, id);
    }

    /**
     * 筛选资源为无效或确定无效
     *
     * @param status           3、无效，4、确定无效
     * @param ids              资源id
     * @param result           备注
     * @param currentLoginUser 操作人
     */
    public void updateInvalidSource(String status, String ids, String result, User currentLoginUser) {
        BizExceptionUtil.isException(StringUtils.isEmpty(ids), "资源id不能为空");
        BizExceptionUtil.isException(StringUtils.isEmpty(result), "备注不能为空");
        BizExceptionUtil.isException(!"3".equals(status) && !"4".equals(status), "操作错误");
        BizExceptionUtil.isException(null == currentLoginUser, "操作人不能为空");
        String idArr[] = ids.split(",");
        for (String id : idArr) {
            SourceBusiness sb = repository.get(SourceBusiness.class,Long.valueOf(id));
            BizExceptionUtil.isException(null == sb, "资源不存在，请刷新后再试");
            BizExceptionUtil.isException(sb.getStatus() != 3 && sb.getStatus()!=0, "资源不存在，请刷新后再试");
            sb.setUpdaterUser(currentLoginUser);
            sb.setResult(result);
            sb.setStatus(Integer.valueOf(status));
            sb.setHandleUserId(currentLoginUser.getId());
            sb.setHandleUser(sb.getUpdaterName());
            repository.saveOrUpdate(sb);
        }
    }
}
